<template>
  <div class="sign_box">
    <div data-v-e2dc4b70="" class="sign_title">
      <div data-v-e2dc4b70="" @click="$router.push('/')" class="back"></div>
      <div data-v-e2dc4b70="" class="head">签到中心</div>
      <div class="rule_icon" @click="rule_show"></div>
    </div>
    <div class="sign_wrap">
      <div class="sign_top">
        <!--<div class="gb_box">
					<div class="gb_font">果币</div>
					<div class="gb_num">{{fruit}}</div>
				</div>-->
        <div class="xj_box">
          <div class="box"></div>
          <div class="box">
            <p class="xj_num"><span>¥</span>{{ money }}</p>
            <p class="xj_font"><span>¥</span>现金</p>
          </div>
          <div class="box">
            <div class="tx_go" @click="$router.push('/cashout')">提现</div>
          </div>
        </div>
        <!--   <div class="xj_font">现金</div>
          <div class="xj_num">￥{{ money }}</div> -->
        <!-- <div
          class="sign_btn"
          :class="[sign_day ? '' : 'over']"
          @click="handleSign"
        ></div> -->
        <!-- <div class="tx_go" @click="$router.push('/cash')"></div> -->
      </div>
    </div>
    <div class="sign_banner">
      <!-- <div class="only_one" v-if="reward_set_list.length === 1">
				<img src="../../assets/img/home/sign_banner.png" alt="" >
				<p class="sign_day_num">您已经累计签到<span>{{running_days}}</span>天</p>
				<Calendar :renderLabel="renderLabel" custom-class="calendar_1" />
			</div> -->
      <!-- 第二种情况 -->
      <div class="author_day">
        <div class="author_day__inner">
          <div class="author_header">
            <p class="sign_day_num_2">
              您已经累计签到<span>{{ running_days }}</span
              >天
            </p>
            <div
              class="sign_btn"
              :class="[sign_day ? '' : 'over']"
              @click="handleSign"
            ></div>
          </div>
          <sign-process
            custom-class="process"
            :data="reward_set_list"
            :active="Number(running_days)"
          />

          <Calendar :renderLabel="renderLabel" custom-class="calendar_2" />
        </div>
      </div>
    </div>
    <!-- 规则弹窗 -->
    <transition name="fade">
      <div class="sign_pop mask" v-show="sign_show">
        <div class="sign_inner">
          <div class="close_pop" @click="close_pop"></div>
          <div class="sign_rule">
            <pre>{{ sign_rule }}</pre>
          </div>
        </div>
      </div>
    </transition>
    <!-- 新的签到弹窗 -->
    <transition name="fade">
      <div class="new_sign_pop mask" @click="close" v-show="new_sign_ok">
        <div class="new_sign_inner">
          <div class="sign_number">
            <span>+</span><span>{{ sign_money }}</span
            ><span>元现金</span>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import Calendar from "../../components/calendar";
import SignProcess from "../../components/signProcess";
import Fetch from "../../utils/fetch";

function isObject(obj) {
  return Object.prototype.toString.call(obj) === "[object Object]";
}

function getRewardSetList(obj) {
  if (isObject(obj)) {
    return Object.keys(obj).map((key) => {
      return {
        value: obj[key].value,
        type: obj[key].type,
        day: key,
      };
    });
  }

  return [];
}

export default {
  data() {
    return {
      sign_show: false,
      sign_rule: "",
      fruit: 0,
      money: 0,
      running_days: 0,
      reward_set_list: [],
      month_signs: {},
      sign_day: true,
      sign_money: 0,
      is_check: false,
      new_sign_ok: false,
    };
  },
  components: {
    Calendar,
    SignProcess,
  },
  mounted() {
    //Fetch('/user/invest/channel');
    this.refresh();
  },
  methods: {
    numFunFruit(startNum, maxNum) {
      var that = this;
      let numText = startNum;
      let golb;
      function numSlideFun() {
        numText += 0.027;
        if (numText >= maxNum) {
          numText = maxNum;
          cancelAnimationFrame(golb);
        } else {
          golb = requestAnimationFrame(numSlideFun);
        }
        that.fruit = parseInt(numText);
      }
      numSlideFun();
    },
    numFunMoney(startNum, maxNum) {
      var that = this;
      let numText = startNum;
      let golb;
      function numSlideFun() {
        numText += 0.027;
        if (numText >= maxNum) {
          numText = maxNum;
          cancelAnimationFrame(golb);
        } else {
          golb = requestAnimationFrame(numSlideFun);
        }
        that.money = numText;
      }
      numSlideFun();
    },
    refresh() {
      Fetch("/user/info/checkinfo").then((res) => {
        if (!res || !res.data) {
          return;
        }
        if (res.data.today_sign == 1) {
          this.sign_day = false;
        }
        const {
          fruit,
          money,
          sign_rule,
          running_days,
          reward_set,
          month_signs,
        } = res.data;
        if (this.is_check) {
          this.numFunFruit(parseInt(this.fruit), parseInt(res.data.fruit));
          this.numFunMoney(parseInt(this.money), res.data.money);
        } else {
          this.fruit = parseInt(res.data.fruit);
          this.money = res.data.money;
        }
        this.sign_rule = sign_rule;
        this.running_days = running_days;
        this.reward_set_list = getRewardSetList(reward_set);
        this.month_signs = month_signs || {};
      });
    },
    handleSign() {
      Fetch("/user/info/checkin").then((res) => {
        this.is_check = true;
        this.refresh();
        this.sign_money = res.data.reward;
        this.new_sign_ok = true;
      });
    },
    //   // <div class="yuan" alt="" />
    renderLabel(day) {
      let str = `<div class="calendar-block">${day}</div>`;

      if (
        Array.isArray(this.month_signs[day]) &&
        this.month_signs[day].length > 0
      ) {
        const temp = this.month_signs[day][0];
        const imgSrc = require("../../assets/img/home/" + temp.type + "2.png");
        str = `
				  <div class="calendar-block">
				   <div class="calendar-block__wrap">
          
              <span class="yuan" alt="" ></span>
				    <div style="color:#ffffff;">${day}</div>
				   </div>
           <div class="calendar-block__label">
             <img class="qiandao" src=" ${imgSrc}" alt="" />x${Number(
          temp[temp.type]
        ).toFixed(0)}<div>
				  </div>
				 `;
      }

      return str;
    },
    rule_show() {
      this.sign_show = true;
    },
    close_pop() {
      this.sign_show = false;
    },
    close() {
      this.new_sign_ok = false;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .van-overlay {
  display: none;
}
/deep/ .van-popup {
  position: static;
}

/deep/ .calendar_block {
  width: 100%;

  &--signed {
    background: url(../../assets/img/home/money2.png) no-repeat center center;
    background-size: 24px 24px;
  }
}
/deep/ .calendar-block {
  width: 100%;
  height: 48px;
  line-height: 48px;
  position: relative;
  text-align: center;

  &__wrap {
    position: relative;

    & > img {
      width: 20px;
      height: 23px;
    }

    & > div {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }
  }

  &__label {
    position: absolute;
    font-size: 12px;
    top: 22px;
    left: 0;
    right: 0;
    color: #f9bfbc;
  }
}
.new_sign_inner {
  width: 255px;
  height: 215px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -122px;
  margin-top: -107px;
  background: url(../../assets/img/home/sign-success.png) no-repeat center
    center;
  background-size: 100% 100%;
  .sign_number {
    width: 100%;
    position: absolute;
    top: 220px;
    text-align: center;
    color: #fff;
    height: 45px;
    line-height: 45px;
    span:first-child,
    span:nth-of-type(2) {
      font-size: 35px;
      height: 45px;
      display: inline-block;
    }
    span:nth-of-type(3) {
      font-size: 17px;
      margin-left: 12px;
      height: 45px;
      vertical-align: top;
      display: inline-block;
    }
  }
}

.calendar_1 {
  width: 93.6%;
  min-height: 348px;
  background: url(../../assets/img/home/date.png) no-repeat center center;
  background-size: 100% 100%;
  margin: 12px auto 0 auto;
  overflow: hidden;
}

.calendar_2 {
  padding-top: 20px;
}

.author_day {
  padding: 0 12px;
  .author_day__inner {
    margin-top: -30px;
    width: 100%;
    min-height: 520px;
    background: url(../../assets/img/home/WechatIMG965.png) no-repeat center
      center;
    background-size: 100% 100%;
  }
}

.process {
  padding-top: 20px;
}

.author_header {
  padding: 11px 18px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-content: center;
  .sign_day_num_2 {
    font-size: 12px;
    font-family: PingFangSC, PingFangSC-Medium;
    font-weight: 500;
    text-align: left;
    line-height: 35px;
    color: #676767;
    span {
      background: #fff4f4;
      color: #ff624b;
      font-weight: bold;
      border: 1px solid #ffbfb6;
      padding: 0 5px;
      border-radius: 2px;
      margin: 0 4px;
    }
  }
  .sign_btn {
    width: 94px;
    height: 34px;
    background: url(../../assets/img/home/sign_btn.png) no-repeat center center;
    background-size: 100% 100%;
    &.sign_btn.over {
      background: url(../../assets/img/home/sign_over.png) no-repeat center
        center;
      background-size: 100% 100%;
      pointer-events: none;
    }
  }
}

.sign_box {
  min-height: 100vh;
  background: #e9991f url(../../assets/img/home/sign_bg.png) no-repeat center
    center;
  background-size: 100% 100%;
  padding-top: 1px;
  padding-bottom: 65px;
}
.sign_title {
  height: 50px;
  width: 100%;
  max-width: 750px;
  background-color: #ffffff;
  font-size: 18px;
  color: #212121;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  z-index: 15;
}
.back {
  width: 10px;
  height: 18px;
  background: url(../../assets/img/home/back_icon.png) no-repeat center center;
  background-size: 100% 100%;
  position: absolute;
  left: 10px;
  top: 16px;
}
.head {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.rule_icon {
  width: 20px;
  height: 20px;
  background: url(../../assets/img/home/rule_icon.png) no-repeat center center;
  background-size: 100% 100%;
  position: absolute;
  right: 15px;
  top: 15px;
}
.sign_wrap {
  margin-top: 50px;
}
.sign_top {
  width: 100%;
  height: 123px;
  position: relative;
  .gb_box,
  .xj_box {
    width: 100%;
    height: 100%;
    text-align: center;
    position: absolute;
    top: 0;
    display: flex;
    .box {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .xj_num {
        font-size: 25px;
        font-family: DINPro, DINPro-Bold;
        font-weight: 700;
        text-align: center;
        color: #ffffff;
        span {
          opacity: 0.8;
          font-size: 14px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: left;
          color: #ffffff;
        }
      }
      .xj_font {
        font-size: 11px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: center;
        color: #fde7e7;
      }
    }
  }
  .gb_box {
    left: 6.66%;
  }

  .gb_font {
    width: 100%;
    line-height: 29px;
    font-size: 12px;
    color: #ffffff;
    margin-top: 9px;
  }

  .tx_go {
    border: 1px solid #ffffff;
    border-radius: 12px;
    font-size: 15px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: center;
    color: #ffffff;
    padding: 2px 16px;
  }
}

.sign_banner {
  width: 100%;
}
.only_one {
  width: 100%;
  margin-top: -7px;
  img {
    width: 100%;
  }
}
.sign_day_num {
  width: 100%;
  line-height: 20px;
  font-size: 16px;
  text-align: center;
  color: #ffffff;
  span {
    color: #de3f6a;
    font-weight: bold;
  }
}

@media (max-width: 360px) {
  .sign_top .gb_num,
  .sign_top .xj_num {
    font-size: 20px;
  }
}
@media (max-width: 414px) and (min-width: 360px) {
  .sign_top .gb_num,
  .sign_top .xj_num {
    font-size: 24px;
  }
}
.mask {
  background: rgba(0, 0, 0, 0.6);
  position: fixed;
  z-index: 99;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.sign_inner {
  width: 236px;
  height: 351px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -175px;
  margin-left: -118px;
  background: url(../../assets/img/home/sign_pop.png) no-repeat center center;
  background-size: 100% 100%;
}
.close_pop {
  width: 31px;
  height: 33px;
  position: absolute;
  top: 0;
  right: 16px;
}
.sign_rule {
  height: 280px;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  position: absolute;
  top: 69px;
  left: 0;
  line-height: 20px;
  font-size: 14px;
  padding: 0 25px;
  pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
/deep/.yuan {
  width: 28px;
  height: 28px;
  background: linear-gradient(315deg, #ff3927 6%, #ff6b4f 101%);
  border-radius: 16px;
  display: inline-block;
  margin-top: 10px;
}
/deep/.qiandao {
  width: 15px;
  height: 15px;
}
/deep/.calendar-block__label {
  width: 40px;

  background: #ffffff;
  box-shadow: 1px 2px 4px 1px #e2e1e1;
  border-radius: 20px;
  margin-top: 12px;
  line-height: 25px;
  vertical-align: middle;
  padding: 0 5px;
  color: #ff624b;
  img {
    vertical-align: middle;
    margin-top: -2px;
    margin-right: 2px;
  }
}
</style>
